package com.haoran.dwz.service.impl;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import com.haoran.dwz.dto.DwzGeneratorDTO;
import com.haoran.dwz.hystrix.UsmHystrixCommandWapper;
import com.haoran.dwz.rest.R;
import com.haoran.dwz.security.AuthenticationHeader;
import com.haoran.dwz.service.DwzService;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
import com.ning.http.client.ListenableFuture;
import com.ning.http.client.Request;
import com.ning.http.client.Response;

public class DwzServiceAsyncHttpClientImpl implements DwzService {

	@Value("${dwz.service.address:http://localhost:8080/}")
	private String SERVICE_ADDRESS;

	@Autowired
	private AuthenticationHeader authenticationHeader;

	@Autowired
	private UsmHystrixCommandWapper usmHystrixCommandWapper;

	@Override
	public ResponseEntity<R> generate(String url, int type) {
		try {
			DwzGeneratorDTO params = new DwzGeneratorDTO(url, type);
			String content = com.alibaba.fastjson.JSONObject.toJSONString(params);
			String serviceUrl = SERVICE_ADDRESS + DWZ_GENERATE;
			Map<String, String> headers = authenticationHeader.create(content);
			Response response = usmHystrixCommandWapper.execute("dwz", "generate", serviceUrl, headers, content,
					HttpMethod.POST);
			String responseBody = response.getResponseBody();
			return ResponseEntity.ok(com.alibaba.fastjson.JSONObject.parseObject(responseBody, R.class));
		} catch (Exception e) {
			return ResponseEntity.badRequest().build();
		}
	}
	
	
//	@RequestMapping(value = "/user")
//    @HystrixCommand(fallbackMethod = "fallback", threadPoolProperties = {  
//            @HystrixProperty(name = "coreSize", value = "30"), @HystrixProperty(name = "maxQueueSize", value = "100"),  
//            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "20") }, commandProperties = {  
//                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),  
//                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "1")  
//  
//    })

	// @Autowired
	// private AsyncHttpClient asyncHttpClient;

	// @Override
	// public ResponseEntity<R> generate(String url, int type) {
	// try {
	// DwzGeneratorDTO params = new DwzGeneratorDTO(url, type);
	// String content = com.alibaba.fastjson.JSONObject.toJSONString(params);
	// String serviceUrl = SERVICE_ADDRESS + DWZ_GENERATE;
	// Map<String, String> headers = authenticationHeader.create(content);
	// BoundRequestBuilder builder = asyncHttpClient.preparePost(serviceUrl);
	// for (Map.Entry<String, String> entry : headers.entrySet()) {
	// builder.addHeader(entry.getKey(), entry.getValue());
	// }
	// builder.setBody(content);
	// Request request = builder.build();
	// ListenableFuture<Response> responseFuture =
	// asyncHttpClient.executeRequest(request);
	// Response response = responseFuture.get();
	// String responseBody = response.getResponseBody();
	// return
	// ResponseEntity.ok(com.alibaba.fastjson.JSONObject.parseObject(responseBody,
	// R.class));
	// } catch (Exception e) {
	// return ResponseEntity.badRequest().build();
	// }
	// }

}
